package com.ssh.manager.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ssh.common.util.Page;
import com.ssh.dao.IDao;
import com.ssh.entity.HelpSale;
import com.ssh.entity.MarketAnalysis;
import com.ssh.manager.MarketAnalysisManager;

@Service
public class MarketAnalysisManagerImpl implements MarketAnalysisManager {
	@Resource
	private IDao daoImpl;

	@Override
	public List<MarketAnalysis> findByCondition(int timeType, int orderBy) {
		List<MarketAnalysis> marketAnalysis = new ArrayList<MarketAnalysis>();
		String sql = "from MarketAnalysis where del!=1 ";
		String orderStr = "";
		if (orderBy==1) {	//品牌分组
			orderStr = " order by product.brand.id, time desc";
		}else {		//超市分组
			orderStr = " order by superMarket.id, time desc";
		}
		
		//获取当前的年月
		Calendar calendar = Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		
		Timestamp stN = new Timestamp(System.currentTimeMillis());
		Timestamp etN = new Timestamp(System.currentTimeMillis());
		Timestamp stP = new Timestamp(System.currentTimeMillis());
		Timestamp etP = new Timestamp(System.currentTimeMillis());
		
		String startStr = "";
		String endStr = "";
		String timeNowS = "";
		String timeNowE = "";
		switch (timeType) {
		case 1:		//月同比
			//拼写本月开始结束时间字符串
			timeNowS = year+"-"+month+"-01 00:00:00";
			timeNowE = "";
			if (month<12) {
				int m = month+1;
				timeNowE = year+"-"+m+"-01 00:00:00";
			}else {
				int y = year+1;
				timeNowE = y+"-01-01 00:00:00";
			}
			//拼写去年本月的开始结束时间
			int pY = year-1;
			startStr = pY+"-"+month+"-01 00:00:00";
			if (month<12) {
				int m = month+1;
				endStr = pY+"-"+month+"-01 00:00:00";
			}else {
				endStr = year+"-01-01 00:00:00";
			}
			
			stN = Timestamp.valueOf(timeNowS);
			etN = Timestamp.valueOf(timeNowE);
			stP = Timestamp.valueOf(startStr);
			etP = Timestamp.valueOf(endStr);
			
			sql += " and (time>=? and time<? or time>=? and time<?) ";
			marketAnalysis = (List<MarketAnalysis>)daoImpl.findListBySql(sql+orderStr, new Object[]{stN,etN,stP,etP}, false, true, false, 0, -1);
			break;
		case 2:		//月环比
			if (month==1) {
				int y = year-1;
				timeNowS = y+"-12-01 00:00:00";
			}else {
				int m = month-1;
				timeNowS = year+"-"+m+"-01 00:00:00";
			}
			if (month<12) {
				int m = month+1;
				timeNowE = year+"-"+m+"-01 00:00:00";
			}else {
				int y = year+1;
				timeNowE = y+"-01-01 00:00:00";
			}
			sql += " and time>=? and time<?";
			stN = Timestamp.valueOf(timeNowS);
			etN = Timestamp.valueOf(timeNowE);
			marketAnalysis = (List<MarketAnalysis>)daoImpl.findListBySql(sql+orderStr, new Object[]{stN,etN}, false, true, false, 0, -1);
			break;
		default:		//年环比
			int ys = year-1;
			int ye = year+1;
			timeNowS = ys+"-01-01 00:00:00";
			timeNowE = ye+"-01-01 00:00:00";
			sql += " and time>=? and time<?";
			stN = Timestamp.valueOf(timeNowS);
			etN = Timestamp.valueOf(timeNowE);
			marketAnalysis = (List<MarketAnalysis>)daoImpl.findListBySql(sql+orderStr, new Object[]{stN,etN}, false, true, false, 0, -1);
			break;
		}
		
		return marketAnalysis;
	}

	@Override
	public void findList(Map conditionMap, Page page, String isDesc) {
		String sql = "select count(*) from MarketAnalysis where del!=1";
		String findSql = "from MarketAnalysis where del!=1";
		try {
			int sum = daoImpl.countAll(sql, null);
			page.setTotalCount(new Long(sum));
			page.countTotalPageNum();
			
			findSql += " order by time "+isDesc;
			List<MarketAnalysis> marketAnalysis = (List<MarketAnalysis>)daoImpl.findListBySql(findSql, new Object[]{}, false, true, false, page.getNumberPerPage()*(page.getCurPageNum()-1), page.getNumberPerPage());
			page.setPageData(marketAnalysis);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<MarketAnalysis> findAll() {
		String sql = "from MarketAnalysis where del!=1 order by time desc";
		List<MarketAnalysis> marketAnalysis = (List<MarketAnalysis>)daoImpl.findListBySql(sql, new Object[]{}, false, true, false, 0, -1);
		return marketAnalysis;
	}

	@Override
	public boolean delete(long id) {
		try {
			MarketAnalysis marketAnalysis = daoImpl.getDomain(MarketAnalysis.class, id);
			daoImpl.deleteDomain(MarketAnalysis.class, id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public MarketAnalysis getById(long id) {
		return daoImpl.getDomain(MarketAnalysis.class, id);
	}

	@Override
	public MarketAnalysis saveUpdate(MarketAnalysis marketAnalysis) {
		return daoImpl.saveOrUpdateDomain(marketAnalysis);
	}

}
